package com.adamall.controller;

import com.adamall.dao.ProductsMapper;
import com.adamall.module.Products;
import com.adamall.module.ProductsExample;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/v1")

public class Products_Controller {
    @Autowired
    ProductsMapper productsMapper;
    ProductsExample productsExample;
    ProductsExample.Criteria criteria;

    //创建商品
    @PostMapping("products")
    public ResponseEntity<Object> Create(@RequestBody Products products) {
        productsMapper.insert(products);
        productsExample = new ProductsExample();

        List<Products> products1 = productsMapper.selectByExample(productsExample);

        return ResponseEntity.status(201).header("Custom-Header", "Status code is 200").body(Response_data.of(products1.get(products1.size() - 1)));
    }

    //更新商品
    @PatchMapping("products/{id}")
    public ResponseEntity<Object> updata(@PathVariable int id, @RequestBody Products products) {
        productsExample = new ProductsExample();
        criteria = productsExample.createCriteria();
        criteria.andIdEqualTo(id);
        productsMapper.updateByExampleSelective(products, productsExample);

        return ResponseEntity.status(200).header("Custom-Header", "Status code is 200").body("更新商品");
    }

    //    验证商品更新成功
    @GetMapping("products/{id}")
    public ResponseEntity<Object> VerifyProductUpdates(@PathVariable int id) {
        return ResponseEntity.status(200).header("Custom-Header", "Status code is 200").body(Response_data.of(productsMapper.selectByPrimaryKey(id)));
    }

}
